package sim.ring.gui.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;

import sim.ring.gui.RingingSimApp;
import sim.ring.gui.RingingSimManager;
import sim.ring.gui.treedata.DataModel;

public class RemoveNodeAction extends Action {
	
	RingingSimApp r;
	
	private static String _CONFIRMREMOVETITLE, _CONFIRMREMOVEMESSAGE;
	
	public RemoveNodeAction(RingingSimApp parent) {
		super();
		this.r = parent;
		setText(r.getLocalisedData("removeNodeAction", "&Remove node@Del"));
		setToolTipText(r.getLocalisedData("removeNodeActionTooltip", "Remove selected node"));
		setImageDescriptor(RingingSimManager.getIMGReg().getDescriptor("removeNode"));
		_CONFIRMREMOVETITLE = r.getLocalisedData("removeConfTitle", "Confirm");
		_CONFIRMREMOVEMESSAGE = r.getLocalisedData("removeConfMessage", "Should the selected node and all dependent nodes be removed?");
	}
	
	@Override
	public void run() {
		if (MessageDialog.openConfirm(
				Display.getCurrent().getActiveShell(), _CONFIRMREMOVETITLE, _CONFIRMREMOVEMESSAGE)) {
			IStructuredSelection selection = (IStructuredSelection)r.getTV().getSelection();
			DataModel selectedElement = (DataModel)selection.getFirstElement();
			r.deleteElementFromDB(selectedElement.getId());
			selectedElement.getParent().remove(selectedElement);
		}
	}

}
